import type { Administrative, AdministrativeParams, ReverseGeocode, SearchPlace } from "@/api/types";
import request from "@/utils/http";

/**
 * 获取行政区划信息(天地图)
 * 接口文档: http://lbs.tianditu.gov.cn/server/administrative2.html
 * tk 需要开放 administrative2 服务权限
 */
const getAdministrativeInfo = ({ keyword, tk, childLevel = 1, extensions = true }: AdministrativeParams): Promise<Administrative.Result> => {
    return request({
        url: "/tianditu/v2/administrative",
        method: "get",
        params: { keyword, tk, childLevel, extensions },
    });
};

/**
 * 地名搜索(天地图)
 * 接口文档: http://lbs.tianditu.gov.cn/server/geocodinginterface.html
 */
const searchPlace = (keyWord: string, tk: string): Promise<SearchPlace.Result> => {
    return request({
        url: `/tianditu/geocoder?ds={"keyWord":"${keyWord}"}&tk=${tk}`,
        method: "get",
    });
};

/**
 * 反向地理编码(天地图)
 * 接口文档: http://lbs.tianditu.gov.cn/server/geocoding.html
 */
const reverseGeocode = (location: LonLat, tk: string): Promise<ReverseGeocode.Result> => {
    return request({
        url: `/tianditu/geocoder?postStr={"lng":${location.lon},"lat":${location.lat},"ver":1}&tk=${tk}&type=geocode`,
        method: "get",
    });
};

/** 地图相关接口 */
export const mapApi = {
    getAdministrativeInfo,
    searchPlace,
    reverseGeocode,
};
